package pl.edu.agh.intobl;

import java.util.List;

import org.apache.log4j.Logger;

public class CustomersList {
	final List<Customer> customers;
	final Double[][] distanceMatrix;
	private Logger log = Logger.getLogger(CustomersList.class);

	public CustomersList(List<Customer> customers) {
		super();
		this.customers = customers;
		this.distanceMatrix = buildDistanceMatrix();
		log.info(customers.get(0));
	}

	private Double[][] buildDistanceMatrix() {
		Double[][] matrix = new Double[customers.size()][customers.size()];
		int n = customers.size();
		for (int i = 0; i < n; i++) {
			matrix[i][i] = 0.0;
			for (int j = i + 1; j < n; j++) {
				Customer customerA = customers.get(i);
				Customer customerB = customers.get(j);
				Double distance = customerA
						.getDistanceFromAnotherCustomer(customerB);
				matrix[i][j] = distance;
				matrix[j][i] = distance;
			}
		}
		return matrix;
	}

	@Override
	public String toString() {
		return "CustomersList customerNo=" + customers.size() + " distanceMatrix="
				+ getDistanceMatrixStringRepresentation();
	}

	private String getDistanceMatrixStringRepresentation() {
		StringBuilder sb = new StringBuilder();
		int n = distanceMatrix.length;
		sb.append("\n");
		for (int i = 0; i < n; i++) {
			sb.append("[");
			for (int j = 0; j < n; j++) {
				sb.append(String.format("%.2f", distanceMatrix[i][j])).append(
						" ; ");
			}
			sb.append("]\n");
		}
		return sb.toString();
	}

	public List<Customer> getCustomers() {
		return customers;
	}
	
	public Customer getCustomerById(int id) {
		return customers.get(id);
	}

	public Double[][] getDistanceMatrix() {
		return distanceMatrix;
	}

	public int getCustomersNumber() {
		return customers.size();
	}
	
	public Customer getDepot() {
		return customers.get(0);
	}
}
